import openpyxl
import datetime
import re
import sys
import os


#  去除头尾空位


#阿拉伯语言下 - 去掉前后换行符

# "%@ and %@ started a private chat"
# "Log in"
# "%@Sent %@ x @ld" =
# "%@ Sent %@ x %ld to %@"
# "If you confirm and end your subscription now, you can still access to it until %@/%@/%@"
# "You now have: %@ coins. The coins will be added to your balance every Monday at GMT 00:00."


#阿拉伯语言下 - 去掉中间空格
#"1.After you send a lucky box, hosts will get one of the above gifts randomly.
#2.You have a chance to open a gift worth up to 999 coins and give hosts a surprise!
#3.Hosts will see the gift animation and receive the value of the opened gift. "

# 去掉汉字 和 中文（）

# 包含 xxx
#"xxx players playing—click to join."
#"%@ has been muted"
#"%@ has been muted by admin"

# 包含 XX
#"Call Price: XX coins/minute"


# 包含 /@  （土耳其下）


class Tool:
    lug_pre = datetime.date.today().strftime("%Y%m%d")

    def open(self):
        print('''你想什么语言
            1、en
            2、ar
            3、tr
            ''')
        tipStr = input().strip()
        if tipStr == '1':
            self.doCreat(0)
        elif tipStr == '2':
            self.doCreat(1)
        elif tipStr == '3':
            self.doCreat(2)
        else:
            print('输入错误')


    def openAndCreat(self):
        print('你想多语言前缀')
        self.lug_pre = input().strip()
        self.doCreat(0)
        self.doCreat(1)
        self.doCreat(2)


    def doCreat(self,index):
        filename = os.path.dirname(__file__)+'/i18N.xlsx'
        if os.path.exists(filename):
            workbook = openpyxl.load_workbook("i18N.xlsx")
            worksheet = workbook.worksheets[0]
            lines = []
            for row in worksheet.iter_rows(min_row=3,min_col=7,max_col=9):
                en = 0
                ar = 1
                tr = 2
                text_sub = "en"
                col1 = row[en].value
                if index == 0 :
                    col3 = row[en].value
                elif index == 1:
                    text_sub = "ar"
                    col3 = row[ar].value
                else :
                    text_sub = "tr"
                    col3 = row[tr].value

                
                if col1 is None:
                    continue
                    
                original_string_1 = str(col1)
                original_string_2 = str(col3)
                #  中文空格变英文空格
                replaced_string_1 = original_string_1.replace(" ", " ")
                
                replaced_string_2 = original_string_2.replace(" ", " ")

                # 去除前后空格
                replaced_string_1 = replaced_string_1.strip()
                replaced_string_2 = replaced_string_2.strip()


                # " 增加转移字符串
                if "\"" in replaced_string_1 and "\\\"" not in replaced_string_1:
                    replaced_string_1 = replaced_string_1.replace("\"", "\\\"")
                    
                if "\"" in replaced_string_2 and "\\\"" not in replaced_string_2:
                    replaced_string_2 = replaced_string_2.replace("\"", "\\\"")
                    
                # 包含中文 中文"（）
                if re.search(r'[\u4e00-\u9fff]', replaced_string_1) is not None:
                    pattern = re.compile(r'[\u4e00-\u9fa5]')
                    replaced_string_1 = pattern.sub('', replaced_string_1)
                    replaced_string_1 = replaced_string_1.replace("（）", "")
                    
                if re.search(r'[\u4e00-\u9fff]', replaced_string_2) is not None:
                    pattern = re.compile(r'[\u4e00-\u9fa5]')
                    replaced_string_2 = pattern.sub('', replaced_string_2)
                    replaced_string_2 = replaced_string_2.replace("（）", "")

                
                #  value 没有值 ， 用 key 来
                if col3 is None:
                    original_string_2 = ""
                    replaced_string_2 = original_string_2.replace(" ", " ")
                
                if replaced_string_2 is None:
                    replaced_string_2 = replaced_string_1
                    
                if not replaced_string_2:
                    replaced_string_2 = replaced_string_1
                    
                if  len(replaced_string_2) == 0:
                    replaced_string_2 = replaced_string_1

                if  replaced_string_2 == "":
                    replaced_string_2 = replaced_string_1
                    
                # \n 开头
                if replaced_string_1.startswith("\n"):
                    replaced_string_1 = replaced_string_1[:1]
                    
                if replaced_string_2.startswith("\n"):
                    replaced_string_2 = replaced_string_2[:1]

                # \n 结尾
                if replaced_string_1.endswith("\n"):
                    replaced_string_1 = replaced_string_1[:-1]
                    
                if replaced_string_2.endswith("\n"):
                    replaced_string_2 = replaced_string_2[:-1]


                #  中间有 \n
                if "\n" in replaced_string_1:
                    replaced_string_1 = replaced_string_1.replace("\n", "")
                    
                if "\n" in replaced_string_2:
                    replaced_string_2 = replaced_string_2.replace("\n", "")
                    
                    
                # 包含有 xxx
                if "xxx" in replaced_string_1:
                    replaced_string_1 = replaced_string_1.replace("xxx", "%d")
                if "xxx" in replaced_string_2:
                    replaced_string_2 = replaced_string_2.replace("xxx", "%d")
                    
                    
                # 包含有 XX
                if "XX" in replaced_string_1:
                    replaced_string_1 = replaced_string_1.replace("XX", "%@")
                if "XX" in replaced_string_2:
                    replaced_string_2 = replaced_string_2.replace("XX", "%@")
                    

                # 包含有 /@
                if "/@" in replaced_string_1:
                    replaced_string_1 = replaced_string_1.replace("/@", "%@")
                if "/@" in replaced_string_2:
                    replaced_string_2 = replaced_string_2.replace("/@", "%@")
                
                line = f'"{replaced_string_1}"="{replaced_string_2}";\n'
                lines.append(line)


            date_string = self.lug_pre+"Localizable"+".strings"

            filename_path = os.path.dirname(__file__)+"/"+text_sub+".lproj"

            if os.path.exists(filename_path):
                os.rmdir(filename_path)
            os.mkdir(filename_path)

            filename_lug = os.path.dirname(__file__)+"/"+text_sub+".lproj"+"/"+date_string
            if os.path.exists(filename_lug):
                os.remove(filename_lug)

            with open(filename_lug,'w',encoding='utf-8') as f:
                f.writelines(lines)
            print(text_sub+":"+date_string+"创建完成")
        else:
            print("error:i18N.xlsx 不存在")



tool = Tool()
tool.openAndCreat()
# tool.open()